/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yami.shop.bean.model.Question;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.dao.QuestionMapper;
import com.yami.shop.service.QuestionService;
/**
 * @author lanhai
 */
@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> implements QuestionService {

	@Autowired
	private QuestionMapper questionMapper;

	@Override
	public Question getDefaultQuestion(String userId) {
		return questionMapper.getDefaultQuestion(userId);
	}

	@Override
	@Transactional(rollbackFor=Exception.class)
	public void updateDefaultQuestion(Long questionId, String userId) {
		questionMapper.removeDefaultQuestion(userId);
		int setCount = questionMapper.setDefaultQuestion(questionId,userId);
		if (setCount == 0) {
			throw new YamiShopBindException("无法修改用户默认地址，请稍后再试");
		}
	}

    @Override
	@CacheEvict(cacheNames = "QuestionDto", key = "#userId+':'+#questionId")
    public void removeQuestionByUserId(Long questionId, String userId) {

    }

	@Override
	@Cacheable(cacheNames = "QuestionDto", key = "#userId+':'+#questionId")
	public Question getQuestionByUserId(Long questionId, String userId) {
		if (questionId == 0) {
			return questionMapper.getDefaultQuestion(userId);
		}
		return questionMapper.getQuestionByUserIdAndQuestionId(userId, questionId);
	}


}
